package cn.tedu.common.redis.config

import org.springframework.context.annotation.Configuration
import org.springframework.boot.context.properties.ConfigurationProperties
import java.util.Set
import java.util.HashSet
import redis.clients.jedis.JedisCluster
import org.apache.commons.pool2.impl.GenericObjectPoolConfig
import redis.clients.jedis.HostAndPort
import org.springframework.context.annotation.Bean

@Configuration
@ConfigurationProperties(prefix="redis.cluster")
class RedisClusterConfig {
  private var nodes: List[String] = _

  @Bean
  def initJedisCluster = {
    val set: Set[HostAndPort] = new HashSet;
    nodes.map { node =>
      val info = node.split(":")
      new HostAndPort(info(0), info(1).toInt)
    }.foreach(set.add(_))
    new JedisCluster(set, prop)
  }
  def prop = {
    val config = new GenericObjectPoolConfig()
    config.setMaxTotal(200); config.setMaxIdle(10); config.setMinIdle(10); config//默认配置
  }

  def getNodes() =  nodes

  def setNodes(nodes: List[String]) { this.nodes = nodes }

}